package com.woniuxy.service.permission.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Permission;
import com.woniuxy.entity.PermissionExample;
import com.woniuxy.mapper.PermissionMapper;
import com.woniuxy.service.permission.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    public int save(Permission permission) {
        return permissionMapper.insertSelective(permission);
    }

    @Override
    public int delete(Integer permissionId) {
        List<Permission> children = getChildrenByPermissionId(permissionId);
        if (!children.isEmpty()){
            throw new RuntimeException("you can't delete a permission which is a parent");
        }

        return permissionMapper.deleteByPrimaryKey(permissionId);
    }

    private List<Permission> getChildrenByPermissionId(Integer permissionId) {
        PermissionExample pe=new PermissionExample();
        pe.or().andParentIdEqualTo(permissionId);

        List<Permission> children = permissionMapper.selectByExample(pe);
        return children;
    }

    @Override
    public int update(Permission permission) {
        return permissionMapper.updateByPrimaryKeySelective(permission);
    }

    @Override
    public List<Permission> findAll() {
        return permissionMapper.selectByExample(null);
    }

    @Override
    public Permission findOne(Integer permissionId) {
        Permission permission = permissionMapper.selectByPrimaryKey(permissionId);
        if (permission==null){
            throw new RuntimeException("can't find Permission by:"+permissionId);
        }
        return permission;
    }

    @Override
    public Permission findRoot() {
        List<Permission> permissions = this.findAll();

        Permission root=null;
        for (Permission permission : permissions) {
            if (permission.getParentId()==-1){
                root=permission;
                break;
            }
        }

        fillChildren(permissions,root);

        return root;
    }

    @Override
    public PageInfo<Permission> findByPage(Integer pageSize, Integer pageNum) {
        List<Permission> permissions = permissionMapper.selectByExample(null);

        PageInfo<Permission> info=new PageInfo<>(permissions);
        return info;

    }

    private void fillChildren(List<Permission> permissions,Permission p){
        List<Permission> children =new ArrayList<>();
        for (Permission permission : permissions) {
            if (permission.getParentId().equals(p.getPermissionId())){
                children.add(permission);
            }
        }
        p.setChildren(children);

        if (p.getChildren().size()>0){
            for (Permission child : children) {
                fillChildren(permissions,child);
            }
        }
    }
}
